﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using System.IO;

namespace WeixinBusiness.Core
{
    public class Common
    {
        /// <summary>
        /// 将对象转换成xml字符串
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="t"></param>
        /// <returns></returns>
        public static string ConvertToXmlString<T>(T t) where T : new()
        {
            string xmlString = string.Empty;

            XmlDocument xd = new XmlDocument();
            using (StringWriter sw = new StringWriter())
            {
                XmlSerializer ser = new XmlSerializer(typeof(T));
                ser.Serialize(sw, t);
                xd.LoadXml(sw.ToString());
                XmlElement element = xd.DocumentElement;
                element.RemoveAllAttributes();
                xmlString = element.OuterXml.ToString();
            }

            return xmlString;
        }

        /// <summary>
        /// 将xml字符串转换成相应的对象
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="XmlString"></param>
        /// <returns></returns>
        public static T ConvertToObject<T>(string XmlString) where T : new()
        {
            try
            {
                XmlSerializer ser = new XmlSerializer(typeof(T));
                object obj = ser.Deserialize(new StringReader(XmlString));
                return (T)obj;
            }
            catch (Exception ex)
            {
                return default(T);
            }
        }

        public static bool SaveXml<T>(string xmlUrl, List<T> list, bool isBackUp = true)
        {
            try
            {
                if (File.Exists(xmlUrl) && isBackUp)
                {
                    var fileName = Path.GetFileName(xmlUrl);
                    var backupName = string.Format("{0}_{1}.backUp", DateTime.Now.ToString("yyyyMMddHHmmsss"), fileName);
                    var backupPath = Path.GetDirectoryName(xmlUrl);
                    var destFile = System.IO.Path.Combine(backupPath, backupName);
                    System.IO.File.Move(xmlUrl, destFile);
                }
                XmlSerializer ser = new XmlSerializer(typeof(List<T>));
                TextWriter tw = new StreamWriter(xmlUrl);
                ser.Serialize(tw, list);
                tw.Close();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return true;
        }

        public static List<T> LoadXml<T>(string xmlUrl)
        {
            if (!File.Exists(xmlUrl))
            {
                return new List<T>();
            }
            List<T> list = new List<T>();
            using (Stream file = File.OpenRead(xmlUrl))
            {
                XmlSerializer ser = new XmlSerializer(typeof(List<T>));
                list = ser.Deserialize(file) as List<T>;
            }
            return list;
        }
    }
}
